package com.zuipin.controller;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.zuipin.entity.WinningRecord;
import com.zuipin.entity.WinningRule;
import com.zuipin.framework.annotation.Valid;
import com.zuipin.framework.annotation.Valids;
import com.zuipin.framework.emuns.ErrorCode;
import com.zuipin.framework.result.PageResult;
import com.zuipin.framework.result.Result;
import com.zuipin.service.IWinningRecordService;
import com.zuipin.service.IWinningRuleService;
import com.zuipin.util.ConstantsUtil;
import com.zuipin.util.Pagination;

import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;

@RestController
@RequestMapping(value = "/winningRule")
public class WinningRuleController {
	@Resource
	private IWinningRuleService		winningRuleService;
	@Resource
	private IWinningRecordService	winningRecordService;
	
	@RequestMapping(value = "/countCanUseNums", method = RequestMethod.GET)
	@ApiOperation(value = "当前可抽奖次数-wuyicheng", notes = "当前可抽奖次数-wuyicheng")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "memberId", value = "会员ID", required = true, dataType = "long", paramType = "query"),
			@ApiImplicitParam(name = "sysId", value = "系统ID", required = true, dataType = "long", paramType = "query") })
	public Result<Map<String, Object>> countCanUseNums(@RequestParam Long memberId,
			@RequestParam Long sysId) {
		Result<Map<String, Object>> result = new Result<>();
		Map<String, Object> map = new HashMap<>();
		long nums = winningRuleService.countCanParticipateNumber(memberId, sysId);
		map.put(ConstantsUtil.CAN_USE_NUMS, nums);
		result.setErrorCode(ErrorCode.SUCCESS);
		result.setData(map);
		return result;
	}
	
	@RequestMapping(value = "/getWinningResult", method = RequestMethod.GET)
	@ApiOperation(value = "获取抽奖结果-wuyicheng", notes = "获取抽奖结果-wuyicheng")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "memberId", value = "会员ID", required = true, dataType = "long", paramType = "query"),
			@ApiImplicitParam(name = "sysId", value = "系统ID", required = true, dataType = "long", paramType = "query") })
	public Result<Map<String, Object>> getWinningResult(@RequestParam Long memberId,
			@RequestParam Long sysId) {
		Result<Map<String, Object>> result = winningRuleService.getWinningResult(memberId, sysId);
		return result;
	}
	
	@RequestMapping(value = "/updateWinningRuleRatio", method = RequestMethod.GET)
	@ApiOperation(value = "修改中奖比例-wuyicheng", notes = "修改中奖比例-wuyicheng")
	@Valids({ @Valid(name = "ruleId", minValue = 2, maxValue = 4),
			@Valid(name = "ratio", minValue = 0, maxValue = 100) })
	@ApiImplicitParams({
			@ApiImplicitParam(name = "updateId", value = "修改者ID", required = false, dataType = "long", paramType = "query"),
			@ApiImplicitParam(name = "ruleId", value = "中奖规则ID,只有2,3,4可修改", required = true, dataType = "long", paramType = "query"),
			@ApiImplicitParam(name = "ratio", value = "中奖比例，只能输入百分比值：如0.10=0.10%", required = true, dataType = "bigDecimal", paramType = "query") })
	public Result<Map<String, Object>> updateWinningRuleRatio(
			@RequestParam(required = false) Long updateId, @RequestParam Long ruleId,
			@RequestParam BigDecimal ratio) {
		Result<Map<String, Object>> result = new Result<>();
		WinningRule winningRule = winningRuleService.findById(ruleId);
		if (winningRule != null) {
			winningRule.setWinningRatio(ratio);
			winningRule.setUpdateId(updateId);
			winningRuleService.update(winningRule);
			result.setErrorCode(ErrorCode.SUCCESS);
		} else {
			result.setErrorCode(ErrorCode.NOT_FIND);
			result.setMessage("WinningRule is not exist.");
		}
		
		return result;
	}
	
	@RequestMapping(value = "/seachWinningRecord", method = RequestMethod.GET)
	@ApiOperation(value = "查询中奖列表-wuyicheng", notes = "查询中奖列表-wuyicheng")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "pageNumber", value = "页码", required = false, dataType = "integer", paramType = "query"),
			@ApiImplicitParam(name = "pageSize", value = "页面大小", required = false, dataType = "integer", paramType = "query") })
	public Result<List<WinningRecord>> seachWinningRecord(
			@RequestParam(required = false) Integer pageSize,
			@RequestParam(required = false) Integer pageNumber) {
		PageResult<List<WinningRecord>> result = new PageResult<>();
		Pagination pagination = new Pagination();
		if (pageNumber == null) {
			pageNumber = Pagination.DEFAULT_PAGE_INDEX;
		}
		if (pageSize == null) {
			pageSize = Pagination.DEFAULT_PAGE_SIZE;
		}
		pagination.setPageNumber(pageNumber);
		pagination.setPageSize(pageSize);
		List<WinningRecord> winningRecords = winningRecordService.searchByPage(pagination);
		if (winningRecords != null && winningRecords.size() > 0) {
			result.setTotal_record(winningRecords.size());
		} else {
			result.setTotal_record(0);
		}
		result.setErrorCode(ErrorCode.SUCCESS);
		result.setData(winningRecords);
		return result;
	}
	
}
